<?php if($_SESSION[Logged_In] == true) {?>
<h3>Upload torrent</h3>
<form method="post" enctype="multipart/form-data">
<table style="border: 1px solid black;">
	<tr>
		<td>Name:</td>
		<td><input type="text" name="name"></td>
	</tr>
	
	<tr>
		<td>Categori</td>
		<td>
		<input type="radio" name="cat" value="1" /></br>
		<input type="radio" name="cat" value="2" /></br>
		<input type="radio" name="cat" value="3" /></br>
		<input type="radio" name="cat" value="4" /></br>
		<input type="radio" name="cat" value="5" /></br>
		<input type="radio" name="cat" value="6" /></br>
		<input type="radio" name="cat" value="7" /></br>
		<input type="radio" name="cat" value="8" /></br>
		<input type="radio" name="cat" value="9" /></br>
		</td>
	</tr>

	<tr>
		<td>Torrent file:</td>
		<td><input type="file" name="upload_torrent"></td>
	</tr>

	<tr>
		<td>Nfo file:</td>
		<td><input type="file" name="upload_nfo"></td>
	</tr>
		
	<tr>
		<td>Submit</td>
		<td><input type="submit" value="Upload torrent"></td>
	</tr>
	
</table>
</form>

<?php
if($_FILES['upload_torrent']['size'] > 0 && $_FILES['upload_nfo']['size'] > 0){
	
	//Kryptera till SHA1
	include('Protocol/BDecode.php');
	include('Protocol/BEncode.php');
	
	//Torrent
	$fileNameTorrent = $_FILES['upload_torrent']['name'];
	$tmpNameTorrent  = $_FILES['upload_torrent']['tmp_name'];
	$fileSizeTorrent = $_FILES['upload_torrent']['size'];
	$fileTypeTorrent = $_FILES['upload_torrent']['type'];
	
	
	//Nfo
	$fileNameNfo = $_FILES['upload_nfo']['name'];
	$tmpNameNfo  = $_FILES['upload_nfo']['tmp_name'];
	$fileSizeNfo = $_FILES['upload_nfo']['size'];
	$fileTypeNfo = $_FILES['upload_nfo']['type'];
	
	//Text & cat
	$namn = mysql_escape_string($_POST["name"]);
	$cat = mysql_escape_string($_POST["cat"]);
	
	//Kollar filer
	
	//Torrent
	if($fileTypeTorrent != "application/x-bittorrent"){
		die("Illegal torrent");
	}
	
	//Nfo - typ
	if($fileTypeNfo != "application/octet-stream"){
		die("Illegal nfo");
	}
	

	//Nfo dattta
	$fpNfo      = fopen($tmpNameNfo, 'r');
	$content_Nfo = fread($fpNfo, filesize($tmpNameNfo));
	$content_Nfo = addslashes($content_Nfo);
	fclose($fpNfo);
	$Nfo = $content_Nfo;

	//Torrent dattta
	$fpTorrent      = fopen($tmpNameTorrent, 'r');
	$content_torrent = fread($fpTorrent, filesize($tmpNameTorrent));
	$content_torrent = addslashes($content_torrent);
	fclose($fpTorrent);
	
	//Sha1
	$tfile = BDecode($content_torrent);
	$sha1 = sha1(BEncode($tfile["info"]));
	
	//Uppladdad av
	$uploaded_by = $_SESSION['Username'];
	
	if(!get_magic_quotes_gpc())
	{
		$fileNameTorrent = addslashes($fileNameTorrent);
	}


	$query = "INSERT INTO torrents (cat, name, data, descr,uploaded_by,sha1) VALUES ('$cat', '$namn','$content_torrent','$Nfo','$uploaded_by','$sha1')";

	mysql_query($query) or die('Error, query failed');

}
}else{
	echo "Need to login!";
}
?>